package io.polly.ldz.reactor.mvc;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ldz112@163.com
 * @Classname TermController
 * @Version V1.0
 * @Description 验证term的三种场景,keyword类型
 * @Date 2024/10/8 13:53
 * Copyright (C) 2023 All Rights Reserved
 */
@RestController
@Slf4j
public class TermController {

    final static String INDEX_NAME = "question";

    @Autowired
    private ElasticsearchClient elasticsearchClient;


    @RequestMapping("/term1")
    public List<Question> term1(){
        List<Question> questions= new ArrayList<>();
        TermQuery termQuery= QueryBuilders.term().field("balance").value(20).build();

        SearchRequest searchRequest = new SearchRequest.Builder().index(INDEX_NAME).query(termQuery._toQuery())
                .build();
        SearchResponse<Question> response=null;

        try {
            response= elasticsearchClient.search(searchRequest, Question.class);
            response.hits().hits().stream().forEach(t -> questions.add(t.source()));

        }
        catch (IOException de){
            System.out.println("Exception handling"+de);
        }

        return questions;
    }

    @RequestMapping("/term2")
    public List<Question> term2(){
        List<Question> questions= new ArrayList<>();
        TermQuery termQuery= QueryBuilders.term().field("tags").value("SingleChoice").build();

        SearchRequest searchRequest = new SearchRequest.Builder().index(INDEX_NAME).query(termQuery._toQuery())
                .build();
        SearchResponse<Question> response=null;

        try {
            response= elasticsearchClient.search(searchRequest, Question.class);
            response.hits().hits().stream().forEach(t -> questions.add(t.source()));

        }
        catch (IOException de){
            System.out.println("Exception handling"+de);
        }

        return questions;
    }

    @RequestMapping("/term3")
    public List<Question> term3(){
        List<Question> questions= new ArrayList<>();
        TermQuery termQuery= QueryBuilders.term().field("title.keyword").value("题目").build();

        SearchRequest searchRequest = new SearchRequest.Builder().index(INDEX_NAME).query(termQuery._toQuery())
                .build();
        SearchResponse<Question> response=null;

        try {
            response= elasticsearchClient.search(searchRequest, Question.class);
            response.hits().hits().stream().forEach(t -> questions.add(t.source()));

        }
        catch (IOException de){
            System.out.println("Exception handling"+de);
        }

        return questions;
    }





}
